//
// Created by kepeiyang on 2021/3/10.
//

#include "TaskPresenter.h"
#include "TaskDao.h"
#include "TaskBean.h"
#include "PlayerPresenter.h"
#include "PlayerDao.h"
#include "PlayerBean.h"
#include <json.h>
#include <iostream>
#include <string>
#include <list>

using std::string;
using std::list;

TaskDao *taskDao;
void getTask(string data, ResData& res_data);
void getPlayerTask(string data, ResData& res_data);
void getPlayerTaskOnce(string data, ResData& res_data);
void addPlayerTask(string data, ResData& res_data);
void updateTaskStatus(string data, ResData& res_data);
void PlayerTaskOver(string data, ResData& res_data);

moduleHandler task_cmds[] = {
        {"01", getTask},
        {"02", getPlayerTask},
        {"03", getPlayerTaskOnce},
        {"04", addPlayerTask},
        {"05", updateTaskStatus},
        {"06", PlayerTaskOver},
};

static string taskList2string(list<PlayerTaskBean> taskList) {
    string out = "";

    for(PlayerTaskBean task: taskList){
        out +=task.toString();
        out += ",";
    }
    int pos = out.rfind(",");
    out.replace(pos,1,"");
    cout << out << endl;
    return out;
}

void getTask(string data, ResData& res_data){
    TaskBean cxTask;
    Json::Value root = Util::init_root(data);

    if(root["taskid"]){
        string taskid = root["taskid"].asString();

        taskDao->getTask(taskid, cxTask);

        if(cxTask.getId() == ""){
            res_data.resp_code = ERRCD_FALSE;
            res_data.resp_data = ERRACKDATA;
        }else{
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = "\"task\":" + cxTask.toString();
        }
    }

}
void getPlayerTask(string data, ResData& res_data){
    list<PlayerTaskBean> cxTaskList;
    Json::Value root = Util::init_root(data);

    if(!root["sid"]){
        return;
    }

    string sid = root["sid"].asString();
    taskDao->getPlayerTaskList(sid, cxTaskList);

    if(cxTaskList.size() == 0){
        res_data.resp_code = ERRCD_FALSE;
        res_data.resp_data = ERRACKDATA;
    }else{
        res_data.resp_code = ERRCD_PASS;
        res_data.resp_data = "\"taskList\":[" + taskList2string(cxTaskList) + "]";
    }
}
void getPlayerTaskOnce(string data, ResData& res_data){
    PlayerTaskBean cxtask;
    Json::Value root = Util::init_root(data);
    if(!root["taskid"]){
        return;
    }

    string sid = root["sid"].asString();
    string taskid = root["taskid"].asString();

    if(!taskDao->getPlayerTaskOnce(sid, taskid, cxtask)){
        res_data.resp_code = ERRCD_NONE;
        res_data.resp_data = NONEACKDATA;
    }else{
        res_data.resp_code = ERRCD_PASS;
        res_data.resp_data = "\"task\":"+ cxtask.toString();
    }
}
void addPlayerTask(string data, ResData& res_data){
    TaskBean cxtask;
    PlayerBean cxplayer;

    Json::Value root = Util::init_root(data);
    if(!root["sid"]){
        return;
    }

    string sid = root["sid"].asString();
    string taskid = root["taskid"].asString();

    taskDao->getTask(taskid, cxtask);

    if(taskDao->addPlayerTask(sid, cxtask)){
        res_data.resp_code = ERRCD_PASS;
        res_data.resp_data = OKACKDATA;
    }else{
        res_data.resp_code = ERRCD_FALSE;
        res_data.resp_data = ERRACKDATA;
    }
}

/**
 * 更新任务状态
 * @param data
 * @param res_data
 */
void updateTaskStatus(string data, ResData& res_data){

    Json::Value root = Util::init_root(data);
    if(!root["sid"]){
        return;
    }

    string sid = root["sid"].asString();
    string taskid = root["taskid"].asString();
    string taskStatus = root["taskStatus"].asString();

    if(taskDao->updateTaskStatus(sid, taskid, taskStatus, 0)){
        res_data.resp_code = ERRCD_PASS;
        res_data.resp_data = OKACKDATA;
    }else{
        res_data.resp_code = ERRCD_FALSE;
        res_data.resp_data = ERRACKDATA;
    }
}
/**
 * 任务完成
 * @param data
 * @param res_data
 */
void PlayerTaskOver(string data, ResData& res_data){
    Json::Value root = Util::init_root(data);
    if(!root["sid"]){
        return;
    }

    string sid = root["sid"].asString();
    string taskid = root["taskid"].asString();

    if(taskDao->updateTaskStatus(sid, taskid, "3", 1)){
        res_data.resp_code = ERRCD_PASS;
        res_data.resp_data = OKACKDATA;
    }else{
        res_data.resp_code = ERRCD_FALSE;
        res_data.resp_data = ERRACKDATA;
    }
}

TaskPresenter::TaskPresenter() {
    taskDao = TaskDao::getInstance();
}
TaskPresenter::~TaskPresenter() {}
void TaskPresenter::TaskMsgHandler(string cmd, string data, ResData &outData) {
    cout << "task: " << data << endl;
    int i = 0;

    if(cmd.empty() || data.empty()){
        return;
    }

    for (i = 0; !task_cmds[i].cmd.empty(); ++i)
    {
        if (cmd != task_cmds[i].cmd)
            continue;
        break;
    }

    task_cmds[i].func(data, outData);
}


